IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[cms_sp_UpdateUser]') AND type in (N'P', N'PC'))
	DROP PROCEDURE [dbo].[cms_sp_UpdateUser]
GO
create procedure [dbo].[cms_sp_UpdateUser]
@UserId int,
@SiteId int = null,
@SiteGroupId int = null,
@LoginName nvarchar(150),
@UserEmail nvarchar(250),
@Firstname nvarchar(250),
@Lastname nvarchar(250),
@PasswordHash nvarchar(150) = null,
@PasswordSalt nvarchar(150) = null,
@IsEnabled bit,
@IsSystem bit,
@LastUpdatedBy int,
@DateLastUpdated datetime
as
update [dbo].[Users]
set [SiteId] = @SiteId,
	[SiteGroupId] = @SiteGroupId,
	[LoginName] = @LoginName,
	[UserEmail] = @UserEmail,
	[Firstname] = @Firstname,
	[Lastname] = @Lastname,
	[IsEnabled] = @IsEnabled,
	[IsSystem] = @IsSystem,
	[LastUpdatedBy] = @LastUpdatedBy,
	[DateCreated] = @DateLastUpdated
where [UserId] = @UserId
declare @ExistingPasswordEquals nvarchar(150)
select @ExistingPasswordEquals = PasswordHash from dbo.UserPasswords
where UserId = @UserId and IsLatestRevision = 1
if (@PasswordHash is not null) and @ExistingPasswordEquals != @PasswordHash
begin
update [dbo].[UserPasswords]
set IsLatestRevision = 0
where UserId = @UserId
insert into [dbo].[UserPasswords]
(
[UserId],
[PasswordHash],
[PasswordSalt],
[IsLatestRevision],
[IsInitial],
[CreatedBy],
[DateCreated]
)
values
(
@UserId,
@PasswordHash,
@PasswordSalt, 
1,
0,
@LastUpdatedBy,
@DateLastUpdated
)
end


GO

